// pages/tools/p-calc/p-calc.page.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    p1: 0,
    p1nb: 0,
    p121: 0,
    p122: 0,
    p2: 0,
    p2nb: 0,
    p221: 0,
    p222: 0,
    c0: 0,
    c1: 0
  },

  customData: {
    c0: 0,
    c1: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {
    wx.setNavigationBarTitle({
      title: '概率计算'
    });
    this.customData.c0 = options.c0 !== undefined ? +options.c0 : 0;
    this.customData.c1 = options.c1 !== undefined ? +options.c1 : 0;
    this.calc(this.customData.c0, this.customData.c1);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  calc(c0: number, c1: number) {
    const t1 = 50 * 49 * 48 * 47 * 46 * 45 * 44;
    const t2 = 43 * 42 * 41 * 40 * 39 * 38 * 37;
    const c1_ = c1 * 7 / 50;
    const p1 = (50 - c0) * (49 - c0) * (48 - c0) * (47 - c0) * (46 - c0) * (45 - c0) * (44 - c0) / t1;
    const p1nb = c0 / 43;
    const p121 = (43 - c0) * (42 - c0) * (41 - c0) * (40 - c0) * (39 - c0) * (38 - c0) * (37 - c0) / t2;
    const p122 = 7 * c0 * (43 - c0 - c1 + c1_)
      * (42 - c0 - c1 + c1_)
      * (41 - c0 - c1 + c1_)
      * (40 - c0 - c1 + c1_)
      * (39 - c0 - c1 + c1_)
      * (38 - c0 - c1 + c1_) / t2;
    const p2 = 7 * c0 * (50 - c0 - c1) * (49 - c0 - c1) * (48 - c0 - c1) * (47 - c0 - c1) * (46 - c0 - c1) * (45 - c0 - c1) / t1;
    const p2nb = (c0 + c1 - 1) / 43;
    const p221 = (44 - c0) * (43 - c0) * (42 - c0) * (41 - c0) * (40 - c0) * (39 - c0) * (38 - c0) / t2;
    const p222 = 7 * (c0 - 1) * (44 - c0 - c1) * (43 - c0 - c1)
      * (42 - c0 - c1) * (41 - c0 - c1) * (40 - c0 - c1) * (39 - c0 - c1) / t2;
    this.setData({
      c0,
      c1,
      p1: p1 < 0 ? 0 : p1 > 1 ? 100 : +(p1 * 100).toFixed(2),
      p1nb: p1nb < 0 ? 0 : p1nb > 1 ? 100 : +(p1nb * 100).toFixed(2),
      p121: p121 < 0 ? 0 : p121 > 1 ? 100 : +(p121 * 100).toFixed(2),
      p122: p122 < 0 ? 0 : p122 > 1 ? 100 : +(p122 * 100).toFixed(2),
      p2: p2 < 0 ? 0 : p2 > 1 ? 100 : +(p2 * 100).toFixed(2),
      p2nb: p2nb < 0 ? 0 : p2nb > 1 ? 100 : +(p2nb * 100).toFixed(2),
      p221: p221 < 0 ? 0 : p221 > 1 ? 100 : +(p221 * 100).toFixed(2),
      p222: p222 < 0 ? 0 : p222 > 1 ? 100 : +(p222 * 100).toFixed(2),
    })
  },

  handleC0Change(e: any) {
    this.customData.c0 = e.detail.value;
    this.calc(this.customData.c0, this.customData.c1);
  },
  handleC1Change(e: any) {
    this.customData.c1 = e.detail.value;
    this.calc(this.customData.c0, this.customData.c1);
  }

})